package pl.smath.expression;

import java.util.Iterator;


public class ProductTerm extends MultiTerm{

	/**
	 * constructor
	 */
	public ProductTerm() {
		super("*");
	}


	/**
	 * @return tekst wyrażenia
	 */
	public Term copy(){
		ProductTerm term = new ProductTerm();
		
		for(int i = 0; i < getTermsList().size(); i++){
			term.addTerm(getTermsList().get(i).copy());
		}
		
		term.setNegative(isNegative());
		return term;
	}

	
	/**
	 * Zamiana do stringa
	 */
	public String toString(){
		String output = "";
		Iterator<Term> terms = getTerms();
		
		while(terms.hasNext()){
			Term term = terms.next();
			
			if(output.length() > 0){
				output += "*";
			}

			if(term instanceof SumTerm)
				output += "(" + term.toString() + ")";
			else
				output += term.toString();
		}
		
		if(isNegative())
			output = "-" + output;
		
		return output;
	}
}
